package cn.koala.key.firstmaintain.controller;

import cn.koala.key.api.FirstMaintainCardApi;
import cn.koala.key.common.enums.EnumKeyValue;
import cn.koala.key.common.excel.ExcelUtils;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.common.utils.BaseEntityUtil;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardDeductParamDTO;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardReceiveParamDTO;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardSearchModel;
import cn.koala.key.firstmaintain.entity.dto.FirstMaintainCardTypeModifyDTO;
import cn.koala.key.firstmaintain.entity.vo.FirstMaintainCardSearchParamVO;
import cn.koala.key.firstmaintain.entity.vo.FirstMaintainCardSearchResultVO;
import cn.koala.key.firstmaintain.service.FirstMaintainCardService;
import cn.seed.common.core.*;
import cn.seed.common.utils.LoggerUtils;
import cn.seed.web.jwt.JwtTokenAuthentication;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 首保卡controller
 *
 * @author liutao
 * @date 2021/1/26
 */
@RestController
@Api(value = "FirstMaintainCardController", tags = "首保卡")
@RequestMapping("/first/card")
public class FirstMaintainCardController {
    @Autowired
    private FirstMaintainCardService firstMaintainCardService;

    @ApiOperation("供管批量收卡")
    @PostMapping("/receive")
    public Result<?> batchReceive(MultipartFile empFile, HttpServletRequest request) {
        List<FirstMaintainCardReceiveParamDTO> list;
        try {
            list = ExcelUtils.importExcel(empFile, FirstMaintainCardReceiveParamDTO.class);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DATA_ERROR, "excel解析异常，请对照模板");
        }
        if (list.isEmpty()) {
            throw new ServiceException(ResultCode.DATA_ERROR, "excel解析为空");
        }
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        ServiceExpUtils.stringIsEmpty(userCode, "操作人工号不允许为空");
        return firstMaintainCardService.batchReceive(BaseEntityUtil.toList(list, FirstMaintainCardReceiveParamDTO::getVinNo), userCode, userName);
    }

    @ApiOperation("批量扣减首保卡次数")
    @PostMapping("/deductCount")
    public Result<?> batchDeductCount(MultipartFile empFile, HttpServletRequest request) {
        List<FirstMaintainCardDeductParamDTO> list;
        try {
            list = ExcelUtils.importExcel(empFile, FirstMaintainCardDeductParamDTO.class);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DATA_ERROR, "excel解析异常，请对照模板");
        }
        if (list.isEmpty()) {
            throw new ServiceException(ResultCode.DATA_ERROR, "excel解析为空");
        }
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        ServiceExpUtils.stringIsEmpty(userCode, "操作人工号不允许为空");
        list.forEach(item -> {
            item.setOprNo(userCode);
            item.setOprName(userName);
        });
        return firstMaintainCardService.batchDeductCount(list);
    }

    @ApiOperation("批量修改首保卡类型")
    @PostMapping("/type/modify")
    public Result<?> batchModifyCardType(MultipartFile empFile, HttpServletRequest request){
        List<FirstMaintainCardTypeModifyDTO> list;
        try {
            list = ExcelUtils.importExcel(empFile, FirstMaintainCardTypeModifyDTO.class);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DATA_ERROR, "excel解析异常，请对照模板");
        }
        if (list.isEmpty()) {
            throw new ServiceException(ResultCode.DATA_ERROR, "excel解析为空");
        }
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        ServiceExpUtils.stringIsEmpty(userCode, "操作人工号不允许为空");
        list.forEach(item -> {
            item.setOprNo(userCode);
            item.setOprName(userName);
        });
        return firstMaintainCardService.batchModifyCardType(list);
    }

    @ApiOperation("首保卡列表")
    @PostMapping("/search")
    public Result<ResultList<FirstMaintainCardSearchResultVO>> searchByCondition(@RequestBody FirstMaintainCardSearchParamVO paramVO) {
        return firstMaintainCardService.searchByCondition(paramVO);
    }

    @ApiOperation(value = "导出首保卡信息")
    @GetMapping("/search/export")
    public void complaintSearchPrint(FirstMaintainCardSearchParamVO firstMaintainCardSearchParamVO, HttpServletResponse response) {
        try {
            List<FirstMaintainCardSearchModel> rModels = firstMaintainCardService.searchPrint(firstMaintainCardSearchParamVO);
            ExcelUtils.exportExcel(response, FirstMaintainCardSearchModel.class, rModels, "首保卡");
        } catch (Exception e) {
            LoggerUtils.error(FirstMaintainCardController.class, e.getMessage());
            throw new ServiceException(ResultCode.FAIL, e.getMessage());
        }
    }


    @ApiOperation("列出首保卡状态")
    @GetMapping("status/list")
    public Result<List<EnumKeyValue>> listCardStatus() {
        return FeignUtils.getHttpApi(FirstMaintainCardApi.class).listCardStatus();
    }

    @ApiOperation("列出首保卡类型")
    @GetMapping("card/type")
    public Result<List<EnumKeyValue>> listCardType() {
        return FeignUtils.getHttpApi(FirstMaintainCardApi.class).listCardType();
    }
}
